Showing posts with label Unix Commands. Show all posts
Showing posts with label Unix Commands. Show all posts

Tuesday, 13 August 2019

How do I add environment variables in Unix



Question: How to print the environment values?
printenv

OR
env



Question: How to SET the environment values For Korn shell (KSH)?
var=value
export varname



Question: How to SET the environment values For Bourne shell (sh and bash)?
export var=value



Question: How to SET the environment values For C shell (csh or tcsh)?
setenv var value



Question: What is .bashrc?
.bashrc is a shell script that Bash runs whenever it is started interactively.


Question: Can we set the environment value in .bashrc?
Yes, you can set the value in this also. like below
export PATH="$PATH:/some/addition"




Wednesday, 25 October 2017

iptables tutorial for Beginner - iptables commands

iptables tutorial for Beginner

Question: What is iptables?
iptables is a utility program that allows a system administrator to configure the tables provided by the Linux kernel firewall.
Different kernel modules and programs are used for different protocols, For Example
iptables applies to IPv4
ip6tables applies to IPv6
arptables applies to ARP
ebtables to Ethernet frames


Question: What privileges required to manage iptables?
iptables requires user root to manage.


Question: How to install iptables?
sudo apt-get install iptables



Question: Why we use iptables?
We use Iptables to allow/deny traffic for incoming/outgoing.


Question: How iptables works?
We define the rules for All type Incoming and Outgoing connection.
For Example
When someone try to established a connection, iptables looks for a rule in its list to do as per rules.


Question: What is Policy Chain default Behavior?
If someone trying to connect and that is not in existing rules, that rules come under default behavior.


Question: How to check all the iptables Rules?
iptables -L -n -v


Output
Chain INPUT (policy ACCEPT 1129K packets, 415M bytes)
pkts bytes target prot opt in out source destination 
0 0 ACCEPT tcp -- lxcbr0 * 0.0.0.0/0 0.0.0.0/0 tcp dpt:53
0 0 ACCEPT udp -- lxcbr0 * 0.0.0.0/0 0.0.0.0/0 udp dpt:53
0 0 ACCEPT tcp -- lxcbr0 * 0.0.0.0/0 0.0.0.0/0 tcp dpt:67
0 0 ACCEPT udp -- lxcbr0 * 0.0.0.0/0 0.0.0.0/0 udp dpt:67


Chain FORWARD (policy ACCEPT 0 packets, 0 bytes)
pkts bytes target prot opt in out source destination 
0 0 ACCEPT all -- * lxcbr0 0.0.0.0/0 0.0.0.0/0 
0 0 ACCEPT all -- lxcbr0 * 0.0.0.0/0 0.0.0.0/0


Chain OUTPUT (policy ACCEPT 354K packets, 185M bytes)
pkts bytes target prot opt in out source destination



Question: What are different type of connection defined in iptables?
  1. Input: This chain is used to control the behavior for incoming connections.
  2. Forward : This chain is used to control the behavior for incoming connections that local but forward from another like router.
  3. Output : This chain is used to control the behavior for outgoing connections



Question: What are different type of iptables Responses for connections?
  1. Accept: Allow the connection.
  2. Drop : Drop the connection. It is used when you don't want the source to realize your system exists
  3. Reject: Don't allow the connection and send back an error.



Question: How to block a connection from specific IP Address?
iptables -A INPUT -s 10.20.20.20 -j DROP
10.20.20.20 is IP Address.


Question: How to block a connection from IP Address range?
iptables -A INPUT -s 10.10.10.0/24 -j DROP

10.20.20.20 is IP Address.


Question: How to un-block a connection from specific IP Address?
iptables -D INPUT -s 10.20.20.20 -j DROP

10.20.20.20 is IP Address.


Question: How to block outgoing connections on a specific port?
iptables -A OUTPUT -p tcp --dport 8082 -j DROP

8082 is Port.


Question: How to block outgoing connections from mulitple port?
iptables -A OUTPUT  -p tcp -m multiport --dports 8081,8082,8083 -j ACCEPT

8081, 8082, 8083 is Port.


Question: How to block incoming connections on a specific port?
iptables -A INPUT -p tcp --dport 8082 -j DROP

8082 is Port.


Question: How to Limit the Number of Concurrent Connections per IP Address?
iptables -A INPUT -p tcp --syn --dport 22 -m connlimit --connlimit-above 3 -j REJECT



Question: How to search a string in iptables?
iptables -L $table -v -n | grep $string



Question: How to add new rule in iptables?
iptables -N custom-filter



Question: How to Disable Outgoing Mails through IPTables?
iptables -A OUTPUT -p tcp --dports 25,465,587 -j REJECT



Question: How to get help on IPTables?
man iptables 



Thursday, 7 September 2017

Shell Scripting Interview Questions and Answer

Shell Scripting Interview Questions and Answer

Question: How to Check if a directory exist?
DIRECTORY='directoryname'
if [ -d "$DIRECTORY" ]; then
  echo "Directory is found"
else
  echo "Directory is Not found"
fi



Question: How to concatenate two string variable?
str1='Web'
str2='Technology'
fullStr="$str1 $str2";
echo $fullStr; #Web Technology 



Question: What do you mean by echo file1.txt > file2.txt?
> is used to redirect output.
echo file1.txt > file2.txt

It just redirect the output to file2.txt


Question: What do you means by 2>&1?
0 is stdin
1 is stdout
2 is stderr
>& is the syntax to redirect a stream to another file descriptor

So, Its saying display the error on stdout


Following two do same thing.
echo test 1>&2
OR
echo test >&2




Question: What is scp and why use? Give example?
SCP is used to copy the file/dir from one server to another server.
Example:
scp -r user@your.server.example.com:/path/to/source /home/user/destination/




Question: What is command to copy a file?
cp file1.txt \another\folder\file1.txt



Question: What is command to move a file?
mv file1.txt \another\folder\file1.txt



Question: How to count number of words in file?
 wc -w file.txt



Question: How to append in file from source file?
 cat sourcefile.txt >> destinationfile.txt
It will add the content in destinationfile.txt from sourcefile.txt



Question: How to search a string and then delete that line?
 sed '/removeTxt/d' ./file
It will remove the line having content removeTxt and print the output to console.



Question: How to set a variable to the output from a command?
 OUTPUT="$(ls -1)"
echo "${OUTPUT}"



Question: How to convert a string in lowercase?
a="Hello World!" 
echo "$a" | tr '[:upper:]' '[:lower:]' #hello world!



Question: What is use of export command?
export command is used to make the available of variable in child process.
Example 
export name=test

Now name variable will be accessible in child process.



Question: How to print date in yyyy-mm-dd format?
YYYY-MM-DD format
echo `date +%Y-%m-%d`
YYYY-MM-DD HH:MM:SS format
echo `date '+%Y-%m-%d %H:%M:%S'`



Question: How to reverse the lines?
tac file1.txt
It will revere the lines and display in console output.


Question: How to redirect output to file?
 ls -l 2>&1 | tee -a errorfile.txt

It will store error and output(both) in error_file.txt


Question: How to determine whether a Linux is 32 bit or 64 bit?
uname -m

Output
x86_64 ==> 64-bit kernel
i686   ==> 32-bit kernel



Question: How to compare two directories?
diff --brief -r dir1/ dir2/
diff -qr -r dir1/ dir2/



Question: When do we need curly braces around shell variables?
When you want to expand the variable (like foo) in the string.
For Example you want to make $foobar.
echo "${foo}bar"




Wednesday, 6 September 2017

Unix Shell Scripting Tutorial - page 8

Unix Shell Scripting Tutorial - page 8

Question: What are two different way to executing a file?
Script file with name script8.sh
myname='Arun kumar';
echo $myname 

Way #1
sh script8.sh

After executing this script, if we run echo $myname it will not print anything because $myname is not in scope.

Way #2
source script8.sh

After executing this script, if we run echo $myname it will print because $myname is in scope.


Question: How to get input from user?
echo "Enter your name?"
read name
echo "Your name is '$name'"



Question: How to include configuration file in main file?
use source to include file.
source config.sh
myname='Arun kumar';
echo $myname 



Question: Give example of for?
for  table in {2..20..2}
do 
echo "$table,"
done

Output 2,4,6,8,10,12,14,16,18,20


Question: What is difference between while and until?
 Until loop always executes at least once. while executes till it returns a zero value and until loop executes till it returns non-zero value.

Question: Give example of function with parameters?
function fname(){
    echo $1; #first argument
    echo $2; #second argument
    echo $3; #third argument
}

fname a b c


Question: Give example of Switch case?
FRUIT="kiwi"

case "$FRUIT" in
   "apple") echo "Apple is good for health." 
   ;;
   
   "banana") echo "I like banana." 
   ;;
   
   "kiwi") echo "New Zealand is famous for kiwi fruit." 
   ;;
esac


Question: What does do eval? Give example eval
execute the statement.
name='name_variable'
name_variable='This is value'
echo $name; #name_variable
echo \${$name}; #${name_variable}
eval echo \${$name}; #This is value

Monday, 4 September 2017

Unix Shell Scripting Tutorial - page 7

Unix Shell Scripting Tutorial - page 7

Question: What is meaning of first line in unix?
#!/bin/bash

This indicates that the script should be run in the bash shell regardless of which interactive shell.


Question: What sign is used in unix for comment a line?
Pound Sign

Pound sign (#), is used to comment a line.

# echo "this is comment" 



Question: When to use single quote OR Double quote?
Single quote: When you are assigning string without variable, then you should use single quote.
Double quote: When you are assigning string with variable, then you should use Double quote.


Question: What's wrong in following variable?
strVar = "Hello World! How are you?" 
echo $strVar;

There should not be space before and after the equal sign (=). Correct code are below:
strVar="Hello World! How are you?" 
echo $strVar;



Question: Give sample example of If else?
if condition1
then
 statement1
 statement2
 statement3
 
else
 statement4
 statement5
fi



Question: Give sample example of If elseif?
if condition1
then
 statement1
 statement2
 statement3
 
elif condition2
then
 statement4
 statement5
 
elif condition3
then
 statement6
 statement7
 statement8
fi



Question: Give list of Operator used in unix shell scripting?
Operator Results number of operands
-noperand non zero length1
-zoperand has zero length1
-dthere exists a directory whose name is operand1
-fthere exists a file whose name is operand1
-eqthe operands are integers and they are equal2
-neqthe opposite of -eq2
=the operands are equal (as strings)2
!=opposite of = 2
-ltoperand1 is strictly less than operand2 (both operands should be integers)2
-gtoperand1 is strictly greater than operand2 (both operands should be integers)2
-geoperand1 is greater than or equal to operand2 (both operands should be integers)2
-leoperand1 is less than or equal to operand2 (both operands should be integers)2



Friday, 1 September 2017

Unix Shell Scripting Tutorial - page 6

Unix Shell Scripting Tutorial - page 6

Question: How can we search in file using Regular expression?
We use grep command.
grep "search string" filename.txt



Question: How to search a string in file?
grep "search string" filename.txt

search string is text searched in filename.txt. By Default its case sensitive.


Question: How to search a case-insensitive string in file?
use -i to search case insensitive.
grep -i "search string" filename.txt



Question: How to search a string in mutiple file?
separate the filename by space.
grep -i "search string" filename.txt filename2.txt filename3.txt 



Question: How to search a string in file using regular expression? Use regex character for example .* used to search all
grep -i "textname .*" filename.txt 

It will return all after the textname.


Question: What are the basic Regex characters?
  1. ? The preceding item is optional and matched at most once.
  2. * matched zero or more times.
  3. + matched one or more times.
  4. {n} matched exactly n times.
  5. {n,} matched n or more times.
  6. {,m} matched at most m times.
  7. {n,m} matched at least n times, but not more than m times.



Question: How to search a words in file?
Use -w to search words.
grep -iw "textname" filename.txt 

It will not search textnameA.


Question: How to search and return next line?
grep -A 3 -i "textname" filename.txt

It will search and return next 3 lines after search.
You can use -B to return Before 3 lines.


Question: How to search recursively in folder? Use -r to search recursive.
grep -r "textname" 



It will search in current folder and sub-folder.


Question: How to find number of matches ? Use -c to return number of matches.
grep -c "textname" fiename.txt

It will return number of matches in number.


Question: What are the Performance Tools in unix?
  1. nice/renice: Runs a program with modified scheduling priority.
  2. netstat: Prints network connections, routing tables and interface statistics.
  3. time: give resource usage
  4. uptime: This is System Load Average.
  5. ps: Current process list
  6. vmstat: Virtual Memory stats
  7. top: Top Task List



Question: What is syslog?
Syslog is a way for network devices to send event messages to a server. Also known as Syslog server.
SysLog is stored in /etc/syslogd OR /etc/syslog.
syslog.conf have following configuration.
*.err;kern.debug;auth.notice /dev/console
daemon,auth.notice           /var/log/messages
lpr.info                     /var/log/lpr.log
mail.*                       /var/log/mail.log
ftp.*                        /var/log/ftp.log
mark.*                       /dev/console



Question: What is command for logging?
logger [-i] [-f file] [-p priority] [-t tag] [message]



Thursday, 31 August 2017

Unix Shell Scripting Tutorial - page 5

Unix Shell Scripting Tutorial - page 5

Question: What are the directory structure of unix?
  1. /: This is the root directory which should contain only the directories.
  2. /bin: Executable files are located here.
  3. /dev: Device drivers
  4. /etc: configuration files, valid user lists, groups, ethernet and hosts etc
  5. /lib: shared library files and sometimes other kernel-related files.
  6. /boot: files for booting the system.
  7. /home: home directory for users and other accounts
  8. /mnt: Used to mount other temporary file systems like CD/pendrive
  9. /proc: Contains all processes marked as a file by process number.
  10. /tmp: Holds temporary files used between system boots.
  11. /user: Used for miscellaneous purposes.
  12. /var: Typically contains log and print files.
  13. /sbin: Contains binary (executable) files.
  14. /kernel: kernel files



Question: How to create a new Group?
groupadd mynewgroup



Question: How to modify group name?
groupmod -n new_modified_group_name old_group_name



Question: How to delete group?
groupdel new_modified_group_name



Question: How to add a existing user to a group?
usermod -a -G mynewgroup username

-G Its for secondary group.
-g Its for primary group.


Question: How to change a User's Primary Group?
usermod -g groupname username  



Question: How to list groups of loggined user?
groups



Question: How to list groups of other user?
groups username



Question: How to Create a New User and Assign a Group?
useradd -G examplegroup username



Question: How to assign a user multiple groups?
usermod -a -G group1,group2,group3 username



Wednesday, 30 August 2017

Unix Shell Scripting Tutorial - page 4

Unix Shell Scripting Tutorial - page 4

Question: What are man pages?
Unix's version of Help files are called man pages.


Question: Can we perform regular expression in unix?
Yes, We can do.
We can do using sed.
Full form of SED is stream editor.
For Example(s)
cat /etc/passwd | sed -n '1,3p'

REGEX_DATE='^\d{2}[/-]\d{2}[/-]\d{4}$'
echo "$1" | grep -P -q $REGEX_DATE
echo $?



Question: What are character meaning in regular expression?
  1. ^a: Start with a
  2. a$: End with a
  3. .: Match any single character.
  4. *: Match zero OR more occurances.
  5. [abcd]: Any character from a,b,c and.
  6. \: Skip the effects of special character.
  7. [a-z]: Match a-z any character.
  8. [a-z0-9]: March a-z OR 0-9 any character.
  9. [[:alnum:]]: Alphanumeric [a-z A-Z 0-9]
  10. [[:alpha:]]: Alphabetic [a-z A-Z]
  11. [[:blank:]]: Blank characters (spaces or tabs)
  12. [[:cntrl:]]: Control characters
  13. [[:digit:]]: Numbers [0-9]
  14. [[:graph:]]: Any visible characters (excludes whitespace)
  15. [[:lower:]]: Lowercase letters [a-z]
  16. [[:print:]]: Printable characters (non-control characters)
  17. [[:space:]]: Whitespace
  18. [[:punct:]]: Punctuation characters
  19. [[:upper:]]: Uppercase letters [A-Z]
  20. [[:xdigit:]]: Hex digits [0-9 a-f A-F]



Question: Can we execute multiple command in single statement?
Yes, We can execute multiple commands in single statement.
For Example
sed -e 'command1' -e 'command2'  -e 'command3'  -e 'command4' 



Question: What are the most comman commands?
  1. cat filename: Displays a filename.
  2. cd dirname: Moves you to the identified directory.
  3. cp file1 file2: Copies one file/directory to the specified location.
  4. file filename: Identifies the file type
  5. head filename: Shows the beginning of a file
  6. less filename:
  7. ls dirname: Display the content of directory.
  8. mv file1 file2: Rename a file
  9. tail filename: Display the end of file
  10. touch filename: Create a blank file.
  11. whereis filename: Display the location of file
  12. :
Question: What is use of df command?
Its used to display the disk space usages.
Filesystem     1K-blocks     Used Available Use% Mounted on
/dev/xvda1      20509288 15036052   5372988  74% /
devtmpfs         2015996       64   2015932   1% /dev
tmpfs            2024996        0   2024996   0% /dev/shm
/dev/xvdf      103081248 44579524  53258844  46% /var/lib/mysql
/dev/xvdg      103081248 89393356   8445012  92% /mnt



Question: What is use of du command?
Its used to display how much space is taking by each directory.


Question: What mount command used for?
mount point is a directory to access your data (files/folders) which is stored in your disks.


Question: What unmount command used for?
To unmount (remove) the file system from your system.
For Example
umount /dev/cdrom



Question: What are the different type of users in unix?
  1. Root User: It is super admin who have all the access and no need to any further permission.
  2. System accounts: It have access of system-specific components like mail accounts and the sshd accounts.
  3. User accounts: User accounts provide interactive access to the system. Users are typically assigned to these accounts and usually have limited access files and directories.



Question: What are the main administrator files?
  1. /etc/passwd: Keeps the user account and password info.
  2. /etc/shadow: Holds the encrypted password.
  3. /etc/group: It have group information.
  4. /etc/gshadow: secure group account information



Monday, 28 August 2017

Unix Shell Scripting Tutorial - page 3

Unix Shell Scripting Tutorial - page 3

Question: What are special variable in unix?
Special variable are those variable which are reserved by unix shell. For Example $$ is special variable and it return the current process id.


Question: How to use array variable in for loop?
Yes, we can define.
NUMS="1 2 3 4 5 6 7 8 9"
for NUM in $NUMS
do
   Q=`expr $NUM % 2`
   if [ $Q -eq 0 ]
   then
      echo "Number is an even number!!"
      continue
   fi
   echo "Found odd number"
done



Question: Give example of while with break?
a=2
while [ $a -lt 10 ]
do
   echo $a
   if [ $a -eq 5 ]
   then
      break
   fi
   a=`expr $a + 1`
done

Once $a will reach to 5, then it will break the statement.
output
2
3
4
5



Question: How to print the special character as string?
Use the Backslash before the special character, It will print. For Example:
echo \$;



Question: What is Output Redirection?
We use > notation to Output Redirection.
who > users
If a command has its output redirected to a file and the file already contains some data, that data will be lost.

We can use >> to append in file, it will not lost the data.
who >> users


Question: What are the Redirection Commands?
  1. pgm > file: Output of pgm is redirected to file.
  2. pgm < file: Program pgm reads its input from file.
  3. pgm >> file: Output of pgm is appended to file.
  4. n > file: Output from stream with descriptor n redirected to file
  5. n >> file: Output from stream with descriptor n appended to file
  6. n >& m: Merges output from stream n with stream m.



Question: How to create a custom function?
# Create a new function with name of Hello
Hello () {
   echo "Hello World! How are you?"
}

# Call the function
Hello



Question: How to create a custom function with parameter?
# Define your function here
Hello () {
   echo "Hello World! How are you?"
   echo "First parameter: $1";
   echo "Second parameter: $2";    
}

# Invoke your function
Hello first second

Output
Hello World! How are you?
First parameter: first
Second parameter: second



Thursday, 24 August 2017

Unix Shell Scripting Tutorial - page 2

Unix Shell Scripting Tutorial - page 2

Question: What are special variable in unix?
Special variable are those variable which are reserved by unix shell. For Example $$ is special variable and it return the current process id.


Question: List the special variable?
  1. $$: Give the current process ID.
  2. $0: File name of current script.
  3. $n: $1 will give first argument, $2 will give 2nd argument.
  4. $#: The number of arguments supplied to a script.
  5. $*: Return all the arguments.
  6. $?: The exit status of the last command executed.
  7. $!: Process number of last command



Question: Can we define array variables?
Yes, we can define.
NAME01="Name 1"
NAME02="Name 2"
NAME03="Name 3"
NAME04="Name 4"
NAME05="Name 5"



Question: How to define array variable and use them?
Define Array variable
NAME01="Name 1"
NAME02="Name 2"
NAME03="Name 3"
NAME04="Name 4"
NAME05="Name 5"

Use single variable
echo $NAME[2];


Print each and every variable
for i in NAME01 NAME02 NAME03 NAME04 NAME05
do
  echo $i;
done



Question: How to print array in single line?
echo  ${NAME[*]};

Question: How to use Relational Operators?
  1. -eq: Equal
  2. -ne: Not Equal
  3. -gt: Greater than
  4. -lt: Less than
  5. -ge: Greater than OR Equal to
  6. -le: Less than OR Equal to



Question: How to use Boolean Operators?
  1. !: Not
  2. -o: OR
  3. -a: And



Question: Give an example of while?
a=1
while [ $a -lt 10 ]
do
   echo $a
   if [ $a -eq 5 ]
   then
      break
   fi
   a=`expr $a + 1`
done



Question: What is Substitution?
The shell performs substitution when it encounters an expression that contains one or more special characters.


Question: Give few example of Variable Substitution?
Variable substitution enables the shell programmer to manipulate the value of a variable based on its state.
Examples
${var}

${var:-word}
If var is null, word is substituted for var.
${var:=word}
If var is null, var is set to the value of word.
${var:?message}
If var is null, message is printed to standard error.
${var:+word}
If var is set, word is substituted for var.


Question: What are the Metacharacters?
metacharacters have special meaning while using them in any Shell Script and causes termination of a word unless quoted.


Following are the meta characters.
    
        * ? [ ] ' " \ $ ;  ( ) | ^ 
    



Wednesday, 23 August 2017

Unix Shell Scripting Tutorial - page 1

Unix Shell Scripting Tutorial - Step 1

Question: How to create file shell script file?
echo "Hello World? How are you all?";



Question: How to make a file executable?
chmod a+rx script1.sh



Question: How to run execute file?
Just with name, you can execute.
script1.sh



Question: How to add comment in shell script files?
Use hash(#) before the line for comment.
chmod a+rx script1.sh



Question: How to use variable in shell script file?
name="Manoj"
echo "My Name is : $name"



Question: What is variable convention?
The name of a variable can contain only letters (a to z or A to Z), numbers ( 0 to 9) or the underscore character ( _).


Question: How to define read only variable?
readonly version="10.2"
echo "Version is : $version"



Question: How to unset variable?
name="Manoj"
unset name;



Question: What are different type of variable?
  1. Local Variables: A local variable is a variable that is present within the current instance of the shell.
  2. Environment Variables: An environment variable is available to any child process of the shell. Some programs need environment variables in order to function correctly.
  3. Shell Variables: A shell variable is a special variable that is set by the shell.



Question: How to use Escape Characters?
Use slash \ for Escape the character;

echo "Hello \"User\""; // Hello "User"
echo "Slash \\" ;// Slash \



Question: How to use expression in shell script file?
Use expr enclosed with caret (~), For Example
myvar=10;
result=`expr 10 * 5`
echo "Multiple: $result";

myvar=10;
result=`expr 10 + 5`
echo "Additon: $result";

myvar=10;
result=`expr 10 -5`
echo "Subtract: $result";

Question: How to use loop?
for i in 1 2 3 4 5
do
  echo "Number $i"
done



Question: How to use if else condition?
if [ 2 = 3 ]
then
  echo "Both are equal"
else
  echo "Both are not equal"
fi



Question: How to use if else elseif condition?
if [ 2 = 3 ]
then
  echo "Both are equal"
elseif [ 2 > 3 ]
  echo "2 is greater than 3"
  else
  echo "2 is less than 3"
fi



Question: How to read from user?
echo "What is your name?"
read name
echo "Your name is $name"


Question: How to search a folder with name?
find ~ -type d -name "*foldername*" -print




Thursday, 28 January 2016

Unix questions and answers for Root user

Unix questions and answers for Root user

Question: How to login as super admin?
Once you login in console, the root account is not active by default.
To be active as super admin, you need to execute following commands.
sudo -su root



Question: How to change the permission of folder?
chmod 755 folder 



Question: How to change the permission of multiple folder?
chmod 755 foldername1 foldername2 foldername2


Question: How to change the permission of folder in Recursive?
chmod -R 755 folder 



Question: How to change the ownership of folder?
sudo chown user:group foldername



Question: How to change the ownership of multiple folder?
sudo chown user:group foldername1 foldername2 foldername2


Question: How to change the ownership of folder in Recursive?
sudo chown -R user:group foldername



Question: How to change the folder permission only not files?
find . -type d -exec chmod 751 {} \;



Question: How to change the folder permission only in Recursive not files?
find . -type d -exec chmod -R 751 {} \;



Question: Who is login is ssh?
whoami



Question: How to change the password of user?
passwd username
(Now it will prompt for new password and re-enter password)


Question: Give example of understanding from 0-7?
Number Read (R) Write (W) Execute (X)
0 No No No
1 No No Yes
2 No Yes No
3 No Yes Yes
4 Yes No No
5 Yes No Yes
6 Yes Yes No
7 Yes Yes Yes



Tuesday, 27 October 2015

SVN interview questions and answers

SVN interview questions and answers


Question: What is SVN?
Subversion is an open source control system which is used to trace all the changes made to your source code.


Question: List out what all things should be stored in SVN repository?
  1. Source Code
  2. MySQL Queries
  3. Database Updates
  4. Project regarding important files
  5. Product Documents
  6. Minutes of Metting and Imp Email


Question: How to add SVN file?
svn add filename.php


Question: How to add SVN folder?
svn add foldername


Question: How to get Updates from SVN Repo
svn update


Question: How to delete file from SVN Repo?
svn delete filename


Question: How to get SVN Info?
svn info


Question: How to get SVN Log for a file?
svn log testFile.php


Question: How to check for modifications?
svn status -v PATH


Question: Difference between SVN commit and SVN update?
SVN commit:: Push (upload) the local changes to Repository.
SVN Update:: Get (download) the Repository changes files to local system.


Question: What is use of Revert in SVN?
Revert your local changes.
It have two types
1) Local Revert: It will delete all changes from files which you made after updates and before commit.
2) Repo Revert: Upload the changes to previous Repo.


Question: List out what is the best practices for SVN?
  1. Work from your own local work-space
  2. Commit small autonomous changes
  3. Use comment
  4. Validate the files you are committing, you actually changed
  5. Take Update before commit to the Repo.



Question: How to apply a patch in SVN ?
First we need to "Create Patch" by making changes and generating the .diff file. Then this .diff file can be applied to the new Code base using "Apply Patch".


Monday, 15 June 2015

Linux and Unix rm command help and examples

rm (short-name for remove) is a command used to remove objects such as files, directories, device nodes, symbolic links etc from the file-system. rm also remove the reference of objects. It works on Linux as well on Unix.


Linux and Unix rm command help and examples

In Actual, rm does not remove any object (file, directories and nodes etc), It just unlink the object from filesystem. That's why file system space may still contain leftover data from the removed file.

Following are rm commands with example.
Delete a single file.
rm filename



Delete all files [USE WITH EXTENSIVE CARE].
rm *



Delete all files which have .txt extension.
rm *.txt 



Delete all files which start with "data" text.
rm data*



Delete a folder (folder must be empty).
rm -d foldername



Delete all recursively. (d-directory, r-recursive).
rm -dr foldername



Delete a file forcefully and will not prompt.
rm -f  filename



Delete the files with prompt (d-directory, r-recursive, i-Prompt before every removal).
rm -dri foldername



Delete the files  without prompt (d-directory, r-recursive, f-without prompt).
rm -drf foldername



Delete the files with without prompt (d-directory, r-recursive, f-without prompt)
rm -drf foldername



Delete the files with single prompt (d-directory, r-recursive, I-single prompt)
rm -drI foldername



Delete the files with details (d-directory, r-recursive, v-explaination)
rm -drv foldername



Delete a file which exist in another folder.
rm /mnt/data/home/project1/public_html/filename.txt 



When we are using multiple options in command, see following:
1) We can exchange the position of option  (Make no difference)
2) We can also give "-" again each option parameter.
3) To Delete a directory recusively, Following commands are valid and are same.
rm -dr foldername
rm -rd foldername
rm -d -r foldername
rm -r -d foldername




Tuesday, 21 April 2015

Unix Commands Interview Questions and Answers for beginner

Unix Commands Interview Questions and Answers for beginner


Question: How to create shell script file?
Follow the simple steps to create first shell script file.
1. Login to putty console with your username/password.
2. Now go inside directory where you want to create the shell script files.
3. type following command to create text file.
vi test1
Now, you are in vi editor mode.
4. Type following script inside editor.
echo "Hello World"

5. Now get out of this vi editor using :wq
6. Now execute your file.
sh test1
7.It should print "Hello World" in output


Question: How can I concatenate two string variables in shell script file?
a="Hello World,"
b=" How r u?"
c=$a$b;
echo $c;#Hello World, How r u?



Question: Give me few examples of Grep command?
Example 1: Search text in file.
grep  "Hello" test.txt

"Hello" -is text, which you want to search.
test.text -It is file where u r searching.

Example 2:Search text in multiple files.
grep "Hello" test_*

"Hello" -is text, which you want to search.
test_* -It means search in all the files which start with test_

Example 3:Search text in file with insensitive.
grep -i "Hello" test.txt
"Hello" -is text, which you want to search.
test_* -It means search in all the files which start with test_

Example 4: Search text recursively in all files.
grep -r "Hello" *
-r: It is option used to search recursively in all sub folder.
"Hello" -is text, which you want to search.
* -Search in all current directory



Question: How to get all cron job for all users?
create empty text file and add following code in that file.
for user in $(cut -f1 -d: /etc/passwd); do crontab -u $user -l; done
Now, just execute the file
sh filename
It will list all the cron job for all user.


Question: What is difference between sh and bash?
Sh: Shell Command is a programming language described by the POSIX standard.
blash: In Starting, bash was also sh compatible but Now it is NOT due to in-valid POSIX shell.


Question: How can I "Reverse the order of lines" in a file?
tail -r text.txt



Question: How to download a file from SSH?
You can copy the file from server to another public directory, from there you can download with FTP/SFTP.
scp username@domain.edu:file.txt /path/to/dir

This command, put the file.txt to "path/to/dir", From this directory you can download.


Question: What is difference between Soft link and Hard link?
Soft Link: soft or symbolic is just a short cut to the original file. If we delete one or more soft link, nothing happens.
Hard link: It is multiple paths to the same file.


Question: How to set screen names with GNU screen?
screen -S foo



Question: How to get full path of a file?
readlink -f test.txt



Question: How to permanently set $PATH on Linux?
You need to add it to your ~/.profile file.
export PATH=$PATH:/path/to/directory



Question: How to list all the files/directory with permission?
 ls -l



Question: How to get the current folder path?
pwd



Question: How can I copy the output of a command directly into my clipboard?
cat file | xclip





Wednesday, 17 September 2014

UNIX Commands Interview Questions and Answers

UNIX Commands Interview Questions and Answers

Question: How to create readonly file?
touch fileName.txt
chmod 400 fileName.txt


Question: How to get the OS in unix?
uname -a


Question: How do you know if a remote host is alive or not?
ping www.example.com
telnet www.example.com


Question: How to copy from one host to another host?
scp file.zip username@hostname:/foldername


Question: How do you find which process is taking how much CPU?
top


Question: How do you check how much space left in current drive
dh-h

Question: How to Restart Apache over the SSH?

/etc/init.d/apache2 restart


Question: How to Stop Apache over the SSH?

/etc/init.d/apache2 stop


Question: How to Start Apache over the SSH?

/etc/init.d/apache2 start


Question: How to Restart Httpd over the SSH?

service httpd restart


Question: How to Stop Httpd over the SSH?

service httpd stop


Question: How to Start Httpd over the SSH?

service httpd start


Question: Import database from sql file to mysql?

mysql –u username –p databasename tablename < sqlfilename.sql


Question: How to delete ALL files including sub directory in current directory with prompting each time?

rm –r *


Question: How to display first 7 lines of file?

head -7 fileName.txt


Question: How to display Last 7 lines of file?

head -7 fileName.txt


Question: How to reverse a string in unix?

echo "Web technology expert notes" | rev


Question: How to unzip a file in Linux?

unzip –j file.zip


Question: How to test if a zip file is corrupted in Linux?
unzip –t file.zip


Question: How to find hidden files in current directory?
ls -lrta


Question: How to check if a file is zipped in Unix?
file file.txt //file.txt: ASCII text


Question: How to check all the running processes in Unix?
ps –ef


Question: find all files in current and subdirectories which contains 'webtechnology' name?
find . -name 'webtechnology'


Question: How to make any script file executable?
chmod 755 *.sh


Question: How to kill process in unix server?
kill -9 #pid

Wednesday, 10 September 2014

Nginx Server interview Questions and Answers

Nginx Server interview Questions and Answers

What is Nginx Server?
Nginx is an open source web server and a reverse proxy server for HTTP, SMTP, POP3, and IMAP protocols with a strong focus on high concurrency, performance and low memory usage. It is pronounced as "engine x".


Who is Author of Nginx?
Igor Sysoev


In which Language Nginx is Written?
'C' Language


What is offical website of Nginx?
nginx.org


When Nginx's stable version launched?
5 August 2014


What is the Best Usage of Nginx Server?
Nginx can deploy dynamic HTTP content on a network with using of SCGI, FastCGI handlers for scripts, WSGI application servers or Phusion Passenger module. Nginx can also serve as a load balancer.


What is the Difference between Apache Web Server and Nginx?
Nginx uses an asynchronous event approach to handling multiple requests whereas Apache Web Server use the synchronous. Nginx's event-driven approach can provide more predictable performance under high loads.

Apache Web Server handling multiple request
|----A-----||-----B-----------||-------C------|

Nginx handling multiple request
 |----A-----|
    |-----B-----------|
        |-------C------|
In this way, Nginx is far better than Apache.



What are Features of Nginx?
  • Simultaneous Connections with low memory
  • Auto Indexing
  • Load Balancing
  • Reverse Proxy with Caching
  • Fault Tolerance



What are all feature of Nginx Server?

  • Ability to handle more than 10,000 simultaneous connections with a low memory.
  • Handling of static files, index files, and auto-indexing
  • Reverse proxy with caching
  • Load balancing with in-band health checks
  • Fault tolerance
  • TLS/SSL with SNI and OCSP stapling support, via OpenSSL.
  • FastCGI, SCGI, uWSGI support with caching
  • FastCGI via PHP (PHP-FPM) support with caching
  • Name- and IP address-based virtual servers
  • IPv6-compatible
  • SPDY protocol support
  • WebSockets and HTTP/1.1 Upgrade (101 Switching Protocols)
  • FLV and MP4 streaming
  • Web page access authentication
  • gzip compression and decompression
  • URL rewriting
  • Custom logging with on-the-fly gzip compression
  • Response rate and concurrent requests limiting
  • Server Side Includes
  • IP address-based Geo-Location
  • User tracking
  • WebDAV
  • XSLT data processing
  • Embedded Perl scripting
  • TLS/SSL support
  • STARTTLS support
  • SMTP, POP3, and IMAP proxy
  • Authentication using an external HTTP server
  • Upgrading executable and configuration without client connections loss and a module-based architecture.



What is the Master and Worker Processes in Nginx Server?
Master process read and evaluate configuration, and maintain worker processes.
Worker processes do actual processing of requests.


Where the Process ID does for Nginx Server? 
The process ID of the master process is written in the file /usr/local/nginx/logs/nginx.pid





What are the controls used in Nginx Server?
Nginx -s [stop | quit | reopen | reload]


What is the purpose of –s with Nginx Server?
-s parameter is used to run the executable file of nginx.


How to add Modules in Nginx Server?
Nginx modules must be selected during compile.
Run-time selection of modules is not supported by Nginx.